const userInfoService = require('../services/user-info');
const userCode = require('../codes/user');
const jwt = require('jsonwebtoken');
const allConfig = require("../config");
const secret = allConfig.secret;

module.exports = {

	/**
	 * 登录操作
	 * @param  {obejct} ctx 上下文对象
	 */
	async signIn(ctx) {
		let formData = ctx.request.body;
		let result = {
			success: false,
			message: '',
			data: null,
			bean: {
				token: ''
			},
			code: ''
		};

		let userResult = await userInfoService.signIn(formData);
		if(userResult) {
			// 用户token
			const userToken = {
				name: userResult.user_name,
				id: userResult.id
			}
			const token = jwt.sign(userToken, secret.sign, {
				expiresIn: '1h'
			}) // 签发token
			result.bean.token = token;
			result.success = true;
			result.data = userResult;
			result.message = '用户登录成功';
		} else {
			result.message = userCode.FAIL_USER_NAME_OR_PASSWORD_ERROR;
			result.code = 'FAIL_USER_NAME_OR_PASSWORD_ERROR';
		}
		if(result.success === true) {
			let session = ctx.session;
			session.isLogin = true;
			session.userName = userResult.user_name;
			session.userId = userResult.id;
		}
		ctx.body = result;
	},

	/**
	 * 注册操作
	 * @param   {obejct} ctx 上下文对象
	 */
	async signUp(ctx) {
		let formData = ctx.request.body;
		let result = {
			success: false,
			message: '',
			data: null
		};

		let validateResult = userInfoService.validatorSignUp(formData);
		if(validateResult.success === false) {
			result = validateResult;
			ctx.body = result;
			return;
		}
		let existOne = await userInfoService.getExistOne(formData);
		if(existOne) {
			if(existOne.name === formData.userName) {
				result.message = userCode.FAIL_USER_NAME_IS_EXIST;
				ctx.body = result;
				return;
			}
		}
		let userResult = await userInfoService.create({
			phone: formData.phone,
			password: formData.password,
			user_name: formData.userName,
			create_time: new Date().getTime(),
			update_time: new Date().getTime(),
		});
		if(userResult && userResult.insertId * 1 > 0) {
			result.success = true;
			result.message = '用户注册成功';
		} else {
			result.message = userCode.ERROR_SYS;
		}
		ctx.body = result;
	},

	/**
	 * 获取用户信息
	 * @param    {obejct} ctx 上下文对象
	 */
	async getLoginUserInfo(ctx) {
		let session = ctx.session;
		let isLogin = session.isLogin;
		let userName = session.userName;
		console.log('session=', session);

		let result = {
			success: false,
			message: '',
			data: null,
		};
		if(isLogin === true && userName) {
			let userInfo = await userInfoService.getUserInfoByUserName(userName);
			if(userInfo) {
				result.data = userInfo;
				result.success = true;
				result.message = '用户信息获取成功';
			} else {
				result.message = userCode.FAIL_USER_NO_LOGIN;
			}
		} else {
			result.message = userCode.FAIL_USER_NO_LOGIN;
		}
		ctx.body = result;
	},

	/**
	 * 校验用户是否登录
	 * @param  {obejct} ctx 上下文对象
	 */
	validateLogin(ctx) {
		let result = {
			success: false,
			message: userCode.FAIL_USER_NO_LOGIN,
			data: null,
			code: 'FAIL_USER_NO_LOGIN',
		}
		let session = ctx.session
		if(session && session.isLogin === true) {
			result.success = true
			result.message = ''
			result.code = ''
		}
		return result
	},

};